<?php
//DbConnection permet de créer une connection avec une BDD
class DbConnection{
    private $host;
    private $dbname;
    private $port;
    private $user;
    private $password;
    private $connection;
    
    public function __construct(array $donnees=array('host'=>'tuxa.sme.utc','dbname'=>'dbnf17p109','port'=>'5432','password'=>'C4Acexyp','user'=>'nf17p109')){
        //la BDD par défaut est celle du compte NF17. Ca évite donc de re-rentrer tous les paramètres à chaque fois
        $this->host=$donnees['host'];
        $this->dbname=$donnees['dbname'];
        $this->port=$donnees['port'];
        $this->user=$donnees['user'];
        $this->password=$donnees['password'];
    }
    public function OpenConnection(){
        $this->connection=pg_connect("host=$this->host port=$this->port dbname=$this->dbname user=$this->user password=$this->password");
    }
    public function CloseConnection(){
        pg_close($this->connection);
    }
    public function GetConnection(){
        return $this->connection;
    }
}

class DbQuery{
    private $dbConnection;
    private $sql;
    private $query;
    public function __construct(DbConnection $dbConnect,$sql_string){
        $this->dbConnection=$dbConnect;
        $this->sql=$sql_string;
    }
    public function SendQuery(){
        $this->query=pg_query($this->dbConnection->GetConnection(), $this->sql);
        return $this->query;
    }
    public function GetResult(){
        return pg_fetch_array($this->query, null, PGSQL_ASSOC);
    }
}
?>
